package com.qdlc.p2p.biz.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper.Nid;


/**
 * 用于session过滤，补充Struts2的sssion拦截器(后台管理员登录地址)
 * 
 * @author ygw
 * 
 */
public class ManageFilter implements Filter {

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		String servletPath = getServletPath(httpServletRequest);
		String adminUrl = SystemConfigHelper.getSystemConfig(Nid.ADMIN_URL).getValue();

		if (!StringUtils.isBlank(adminUrl) && servletPath.startsWith(adminUrl)) {
			request.setAttribute("source", "fliter");
			// 地址改成相应的HomeAction中adminUrl()的地址
			httpServletRequest.getRequestDispatcher(
					"/admin/xGrdsEVQDNT2rIOP.html").forward(request, response);
		} else {
			httpServletResponse.sendRedirect(httpServletRequest
					.getContextPath() + "/notfound.html");
		}
	}
	
    protected String getServletPath(HttpServletRequest request){
    	String servletPath=request.getRequestURI();
		return servletPath;
	}
}